package com.credithc.eagleeye.api.web;
import com.credithc.eagleeye.api.protocol.LoginReq;
import com.credithc.eagleeye.api.protocol.LoginRes;
import com.credithc.eagleeye.api.service.SettingService;
import com.credithc.eagleeye.api.utils.JWTUtils;
import com.credithc.eagleeye.api.utils.ServletUtils;
import com.credithc.eagleeye.api.vo.AuthTokenVo;
import com.credithc.eagleeye.api.vo.PlatformVo;
import com.credithc.eagleeye.api.vo.ResultVo;
import com.credithc.eagleeye.api.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
/**
 * 描 述: <br/>
 * 作 者: zhangjin<br/>
 * 创 建：2018年02月07日<br/>
 * 版 本：v1.0.0<br>
 * <p>
 * 历 史: (版本) 作者 时间 注释 <br/>
 */
@SuppressWarnings("Duplicates")
@Slf4j
@Controller
public class UserApiCtrl extends BaseApiCtrl {
    @Autowired
    private SettingService settingService;

    @RequestMapping(value = "/status", method = RequestMethod.GET)
    @ResponseBody
    public ResultVo status() {
        ResultVo resultVo = new ResultVo<>();
        resultVo.setCode("0000");
        resultVo.setMsg("成功");
        return resultVo;
    }

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo<LoginRes> login(@RequestBody LoginReq loginReq) {
        ResultVo<LoginRes> resultVo = new ResultVo<>();
        resultVo.setCode("0000");
        resultVo.setMsg("成功");
        UserVo userVo =null;
        try {
            userVo = settingService.loadUser(loginReq.getUserName());
        } catch (Exception ex) {
        }
        if (userVo != null && StringUtils.equals(userVo.getPassword(), loginReq.getPassword())) {
            AuthTokenVo tokenVo = new AuthTokenVo();
            tokenVo.setSessionId(UUID.randomUUID().toString().replaceAll("-", ""));
            tokenVo.setUserName(loginReq.getUserName());
            LoginRes loginRes = new LoginRes();
            String token = JWTUtils.getInstance().encodeJWT(tokenVo, TimeUnit.DAYS.toMillis(1));
            loginRes.setToken(token);
            resultVo.setData(loginRes);
        } else {
            resultVo.setCode("9002");
            resultVo.setMsg("用户名或密码不正确!");
        }
        return resultVo;
    }
    @RequestMapping(value = "/logout", method = RequestMethod.POST)
    @ResponseBody
    public ResultVo logout() {
        ResultVo resultVo = new ResultVo<>();
        resultVo.setCode("0000");
        resultVo.setMsg("成功");
        return resultVo;
    }

    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    @ResponseBody
    public ResultVo<UserVo> getUserInfo(HttpServletRequest request) {
        ResultVo<UserVo> resultVo = new ResultVo<>();
        resultVo.setCode("0000");
        resultVo.setMsg("成功");
        //获取token
        String token = ServletUtils.getRequestToken(request);
        try {
            AuthTokenVo authTokenVo = JWTUtils.getInstance().decodeJWT(token);
            UserVo userVo = settingService.loadUser(authTokenVo.getUserName());
            if(userVo.getUserName().equalsIgnoreCase("admin")) {
                userVo.setRoles(new String[]{"admin"});
                List<PlatformVo> platformVoList = this.settingService.loadAllPlatform();
                PlatformVo platformVo = new PlatformVo();
                platformVo.setName("");
                platformVo.setDescribe("全部");
                if (!platformVoList.contains(platformVo)) {
                    platformVoList.add(platformVo);
                }
                userVo.setPlatformVoList(platformVoList);
            }else{
                userVo.setRoles(new String[]{"guest"});
                List<PlatformVo> platformVoList = new ArrayList<>();
                for(String platform : userVo.getPlatformList()) {
                    PlatformVo platformVo = this.settingService.loadPlatform(platform);
                    platformVoList.add(platformVo);
                }
                userVo.setPlatformVoList(platformVoList);
            }
            resultVo.setData(userVo);
        } catch (Exception ex) {
            resultVo.setCode("9001");
            resultVo.setMsg("token无效");
        }

        return resultVo;
    }
}
